<?php
ini_set('display_errors','On');
header("Content-Type: text/html;charset=utf-8");
include("../function/_nosql.php"); //
include("../function/function.php");
// require_once "../weipay/lib/WxPay.Api.php";


// //①、获取用户openid
// $tools = new JsApiPay();
// $openId = $tools->GetOpenid();
// $id = $openId.",".time();
// $txt = 'access_token.txt';
// $access_token = file_get_contents($txt);
// if($access_token == ''){
//     file_put_contents('access_token.txt', $id);
// }else{
//     $access_token = explode(",",$access_token);
//     if(time() - $access_token[1] > 7200){
//         $url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".WxPayConfig::APPID."&secret=".WxPayConfig::APPSECRET;
//         $res = curlget($url);
// 		$res = json_decode($res,true);
// 		$access_token = $res['access_token'];
// 		file_put_contents('access_token.txt', $res['access_token'].",".time());
//     }else{
//         $access_token = $access_token[0];
//     }
// }
//         $url ="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openId."&lang=zh_CN";
//         $nema = curlget($url);
//         $nema = json_decode($nema,true);
function str_count($sql,$str){
 $res = mysql_query($sql);

 $num = mysql_num_rows($res);

 $count = 0;

  if($num>0){

	while($arr=mysql_fetch_array($res)){

	 $count = $count + $arr[$str];

	}

  }

  return $count;

}
if(IS_AJAX){
    $id = chkstr(trim($_REQUEST['id']));
    $hid = chkstr(trim($_REQUEST['hid']));
    $openId = chkstr(trim($_REQUEST['openId']));
	             $jri = date("d",time());
	            $jyue = date("m",time());
	        $piao_zshu = getClassName("kboy_piao","piao_shu","id='$id' and tuid = 1");
            $piao_zshu = $piao_zshu + 1;
            $cai_tou = getClassName("kboy_saishi","cai_tou","id='$hid'");
            $cai_tou = $cai_tou + 1;
            	            $piao_shus = getNewsNums("kboy_weipiao","piao_wid='$openId' and piao_paid = '$id' and piao_hid = '$hid' and piao_ri = '$jri' and piao_yue = '$jyue'");
            	            if($piao_shus == 0){
            	                $zongliu = str_count("select * from kboy_weipiao where piao_wid='$openId' and piao_hid = '$hid' and piao_ri = '$jri' and piao_yue = '$jyue' ","piao_wshu");
                    	        if($zongliu > 4){
                    	            echo 1;
                    	        }else{
            	                $mysql = new sqlstr("kboy_saishi"); 
                        		$mysql -> set("cai_tou",$cai_tou);
                                $news_sql = $mysql -> updateSql("id='$hid'");
                        		$news_res = mysql_query($news_sql);
            	                
            	                $mysql = new sqlstr("kboy_piao"); 
                        		$mysql -> set("piao_shu",$piao_zshu);
                                $news_sql = $mysql -> updateSql("id='$id'");
                        		$news_res = mysql_query($news_sql);
                        		
                        		$mysql = new sqlstr("kboy_weipiao"); 
                        		$mysql -> set("piao_wshu",1);
                        		$mysql -> set("piao_paid",$id);
                        		$mysql -> set("piao_hid",$hid);
                        		$mysql -> set("piao_wid",$openId);
                                $mysql -> set("piao_ri",$jri);
                		        $mysql -> set("piao_yue",$jyue);
                                $news_sql = $mysql -> insertSql();
                        		$news_res = mysql_query($news_sql);
                        		if($news_res){
                        		    echo json_encode(array('id'=>2,'shu'=>$piao_zshu));
                        		}
                    	        }
            	            }else{
            	                $zongliu = str_count("select * from kboy_weipiao where piao_wid='$openId' and piao_hid = '$hid' and piao_ri = '$jri' and piao_yue = '$jyue' ","piao_wshu");
                    	        if($zongliu > 4){
                    	            echo 1;
                    	        }else{
                    	           $piao_shu = getClassName("kboy_weipiao","piao_wshu","piao_wid='$openId' and piao_paid = '$id' and piao_ri = '$jri' and piao_yue = '$jyue'");
                	                $piao_shu = $piao_shu + 1;
                	                $mysql = new sqlstr("kboy_saishi"); 
                            		$mysql -> set("cai_tou",$cai_tou);
                                    $news_sql = $mysql -> updateSql("id='$hid'");
                            		$news_res = mysql_query($news_sql);
                	                
                	                $mysql = new sqlstr("kboy_piao"); 
                            		$mysql -> set("piao_shu",$piao_zshu);
                                    $news_sql = $mysql -> updateSql("id='$id'");
                            		$news_res = mysql_query($news_sql);
                	                
                	                $mysql = new sqlstr("kboy_weipiao");
                            		$mysql -> set("piao_wshu",$piao_shu);
                                    $news_sql = $mysql -> updateSql("piao_wid='$openId' and piao_paid = '$id' and piao_ri = '$jri' and piao_yue = '$jyue'");
                            		$news_res = mysql_query($news_sql);
                            		if($news_res){
                            		    echo json_encode(array('id'=>2,'shu'=>$piao_zshu));
                            		}   
                    	        }
            	            }
}

















?>